﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using GameEditor.Common.Controls;
using TaskSystemClient.SPM;
using GameEditor.Common;
using GameEditor.Common.ComponentModel;
namespace TaskSystemClient
{
    public class TaskTreeViewHelper
    {

        static public List<string> splitCatalog(string catalog)
        {
            List<string> t = new List<string>();
            if (String.IsNullOrEmpty(catalog))
            {
                return t;
            }
            if (catalog.IndexOf(')') == -1)
            {
                t.Add(catalog);
                return t;
            }

            while (catalog.Length > 0)
            {
                int s = catalog.IndexOf(')');
                if (s == -1)
                {
                    t.Add(catalog);
                    break;
                }
                int length = s - 1;
                if (length < 0) length = 0;
                string temp = catalog.Substring(1, length);
                catalog = catalog.Substring(s + 1);
                t.Add(temp);
            }
            return t;
        }

        public static void getStyleByTaskState(Task task, out FontStyle fontStyle, out Color foreColor)
        {
            fontStyle = FontStyle.Regular;
            foreColor = Color.Black;


            if (string.Compare(task.State, TaskState.Close, true) == 0)
            {
                fontStyle = FontStyle.Strikeout;
                foreColor = Color.DarkSlateGray;
            }
            else if (string.Compare(task.State, TaskState.Checking, true) == 0)
            {
                //fontStyle = FontStyle.Bold;
                fontStyle = FontStyle.Underline;
                foreColor = Color.DarkGoldenrod;
                //foreColor = Color.DarkRed;
            }
            else if (string.Compare(task.State, TaskState.Done, true) == 0)
            {
                fontStyle = FontStyle.Bold;
                foreColor = Color.DarkRed;
            }
            else if (string.Compare(task.State, TaskState.Reopen, true) == 0)
            {
                //foreColor = Color.MidnightBlue;
            }
            else if (string.Compare(task.State, TaskState.Repeat, true) == 0)
            {
                foreColor = Color.Gray;
                fontStyle = FontStyle.Strikeout | FontStyle.Italic;
            }
            else if (string.Compare(task.State, TaskState.Discard, true) == 0)
            {
                foreColor = Color.Gray;
                fontStyle = FontStyle.Strikeout | FontStyle.Italic;
            }
            else
            {
                fontStyle = FontStyle.Regular;
                foreColor = Color.Black;
            }
        }

        static public NodeCell getNodeCell(TreeListNode node, ETaskField field)
        {
            foreach (TreeListColumn tlc in node.TreeListView.Columns)
            {
                TaskColumnInfo ci = tlc.Tag as TaskColumnInfo;
                if (ci == null)
                {
                    return null;
                }
                if (ci.FieldID == field)
                {
                    return node[tlc.Index];
                }
            }
            return null;
        }
        static public void UpdateTaskToTreeListNode(Task task, TreeListNode n)
        {
            TreeListView treeList = n.TreeListView;
            if (treeList == null)
            {
                return;
            }

            List<string> fieldsValue = new List<string>();
            foreach (TreeListColumn tlc in treeList.Columns)
            {
                TaskColumnInfo ci = tlc.Tag as TaskColumnInfo;
                if (ci == null)
                {
                    fieldsValue.Add("<!!>");
                    continue;
                }

                switch ((int)ci.FieldID)
                {
                    case (int)ETaskField.etfTaskID:
                        {
                            fieldsValue.Add(task.ID.ToString());
                        }
                        break;
                    case (int)ETaskField.etfTaskTitle:
                        {
                            fieldsValue.Add(task.Title);
                        }
                        break;
                    case (int)ETaskField.etfTaskVersion:
                        {
                            fieldsValue.Add(task.Version.ToString());
                        }
                        break;
                    case (int)ETaskField.etfTaskType:
                        {
                            fieldsValue.Add(task.Type ?? "<null>");
                        }
                        break;
                    case (int)ETaskField.etfTaskDescription:
                        {
                            string plainText = "";
                            if (!string.IsNullOrEmpty(task.Description))
                            {
                                plainText = Utilities.ExtractTextFromHTML(task.Description);
                            }
                            fieldsValue.Add(plainText);
                        }
                        break;
                    case (int)ETaskField.etfTaskLinks:
                        {
                            string plainText = "";
                            if (!string.IsNullOrEmpty(task.Links))
                            {
                                plainText = task.Links;
                                //plainText = Utilities.ex(task.Description);
                            }
                            fieldsValue.Add(plainText);
                        }
                        break;
                    case (int)ETaskField.etfTaskLastModifyTime:
                        {
                            string dateTimeString = TranslateLastModifyTime(task.LastModifyTime);
                            fieldsValue.Add(dateTimeString);

                            if (task.LastModifyTime != null)
                            {
                                DateTime now = DateTime.Now;
                                TimeSpan diff = now - task.LastModifyTime;
                                int days = diff.Days;
                                Color backColor = Color.White;
                                if (days == 0)
                                    backColor = Color.Yellow;
                                else if (days == 1)
                                    backColor = Color.YellowGreen;
                                else if (days == 2)
                                    backColor = Color.Silver;
                                else if (days <= 7)
                                    backColor = Color.DimGray;
                                else if (days <= 15)
                                    backColor = Color.SlateGray;
                                NodeCell cell = getNodeCell(n, ETaskField.etfTaskLastModifyTime);
                                if (cell != null)
                                {
                                    cell.BackColor = backColor;
                                }
                            }
                        }
                        break;
                    case (int)ETaskField.etfTaskLastModifyUser:
                        {
                            fieldsValue.Add(task.LastModifyUser ?? "<null>");

                            if (Global<MainForm>.Instance.UserName.Equals(task.LastModifyUser))
                            {
                                NodeCell cell = getNodeCell(n, ETaskField.etfTaskLastModifyUser);
                                if (cell != null)
                                {
                                    cell.BackColor = Color.Tomato;
                                }
                            }
                        }
                        break;
                    case (int)ETaskField.etfTaskState:
                        {
                            fieldsValue.Add(task.State ?? "<null>");
                        }
                        break;
                    case (int)ETaskField.etfTaskPriority:
                        {
                            string priorityString = task.Priority ?? "<null>";
                            fieldsValue.Add(priorityString);
                            //依据优先级来设置单元格颜色 
                            NodeCell cell = getNodeCell(n, ETaskField.etfTaskPriority);
                            if (cell != null)
                            {
                                cell.ForeColor = Color.Wheat;
                                for (int i = 0; i < TaskPriority.Priorities.Length; i++)
                                {
                                    if (string.Compare(priorityString, TaskPriority.Priorities[i], true) == 0)
                                    {

                                        if (cell != null)
                                        {
                                            cell.BackColor = TaskPriorityEditorDelegate.Colors[i];
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                        break;
                    case (int)ETaskField.etfTaskUsers:
                        {
                            fieldsValue.Add(task.Users_ForDisplay ?? "");
                        }
                        break;
                    case (int)ETaskField.etfTaskTags:
                        {
                            fieldsValue.Add(task.Tags_ForDisplay ?? "");
                        }
                        break;
                    case (int)ETaskField.etfTaskStartTime:
                        {
                            string dateTimeString = TaskStartTimeEditorDelegate.FormatDateTime(task.StartTime);
                            fieldsValue.Add(dateTimeString);
                        }
                        break;
                    case (int)ETaskField.etfTaskFinishTime:
                        {
                            string dateTimeString = TaskFinishTimeEditorDelegate.FormatDateTime(task.FinishTime);
                            fieldsValue.Add(dateTimeString);
                        }
                        break;
                    case (int)ETaskField.etfTaskCreateTime:
                        {
                            string dateTimeString = TaskStartTimeEditorDelegate.FormatDateTime(task.CreateTime);
                            fieldsValue.Add(dateTimeString);
                        }
                        break;
                    case (int)ETaskField.etfTaskCreateUser:
                        {
                            fieldsValue.Add(task.CreateUser);
                        }
                        break;
                    case (int)ETaskField.etfMileStoneCode:
                        {
                            fieldsValue.Add(task.MileStoneCode ?? "");
                        }
                        break;
                    case (int)ETaskField.etfTaskCatalog1:
                        {
                            fieldsValue.Add(task.GetCatalog(ci.FieldID) ?? "");
                        }
                        break;
                    case (int)ETaskField.etfTaskCatalog2:
                        {
                            fieldsValue.Add(task.GetCatalog(ci.FieldID) ?? "");
                        }
                        break;
                    case (int)ETaskField.etfTaskCatalog3:
                        {
                            fieldsValue.Add(task.GetCatalog(ci.FieldID) ?? "");
                        }
                        break;
                    case (int)ETaskField.etfTaskCatalog4:
                        {
                            fieldsValue.Add(task.GetCatalog(ci.FieldID) ?? "");
                        }
                        break;
                    case (int)ETaskField.etfTaskCatalog5:
                        {
                            fieldsValue.Add(task.GetCatalog(ci.FieldID) ?? "");
                        }
                        break;
                    case (int)ETaskField.etfTaskCatalog6:
                        {
                            fieldsValue.Add(task.GetCatalog(ci.FieldID) ?? "");
                        }
                        break;
                    case (int)ETaskField.etfTaskCatalog7:
                        {
                            fieldsValue.Add(task.GetCatalog(ci.FieldID) ?? "");
                        }
                        break;
                    case (int)ETaskField.etfTaskCatalog8:
                        {
                            fieldsValue.Add(task.GetCatalog(ci.FieldID) ?? "");
                        }
                        break;

                    case (int)ETaskField.etfTaskEstimatedTime: 
                        {
                            fieldsValue.Add(task.EstimatedTime == 0 ? "" : task.EstimatedTime.ToString());
                        }
                        break;
                    case (int)ETaskField.etfTaskCostedTime:
                        {
                            fieldsValue.Add(task.CostedTime == 0 ? "" : task.CostedTime.ToString());
                        }
                        break;
                    case (int)ETaskField.etfTaskUserForChecking:
                        {
                            fieldsValue.Add(task.UserForChecking??"");
                        }
                        break;
                    default:
                        {
                            fieldsValue.Add(string.Format("未知字段:%d", (int)(ci.FieldID)));
                        }
                        break;
                }
            }
            n.SetFields(fieldsValue.ToArray());
        }
        static public string TranslateLastModifyTime(DateTime dateTime)
        {
            if (dateTime == null)
            {
                return "<null>";
            }
            DateTime now = DateTime.Now;
            TimeSpan diff = now - dateTime;
            int days = diff.Days;
            if (days == 0)
            {
                if (now.Day != dateTime.Day)
                {
                    return "昨天 " + dateTime.ToShortTimeString();
                }
                else
                {
                    return "今天 " + dateTime.ToShortTimeString();
                }
            }
            if (days == 1)
            {
                return "昨天 " + dateTime.ToShortTimeString();
            }
            if (days == 2)
            {
                return "前天 " + dateTime.ToShortTimeString();
            }
            if (days <= 7)
            {
                return "一周内 " + dateTime.ToShortDateString();
            }
            if (days <= 15)
            {
                return "半月内 " + dateTime.ToShortDateString();
            }
            return dateTime.ToString();
        }

    }
}
